package com.zy.xuanwo.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zy.xuanwo.R;
import com.zy.xuanwo.acitivity.AccountManagementActivity;
import com.zy.xuanwo.acitivity.CommonProblemActivity;
import com.zy.xuanwo.acitivity.LoginActivity;
import com.zy.xuanwo.application.MyApplication;
import com.zy.xuanwo.utils.ToastUtils;
import com.zy.xuanwo.acitivity.ProfileActivity;
import com.zy.xuanwo.utils.UserInfoSPManager;
import com.zy.xuanwo.view.TitleView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @功能 设置
 * Created by njb on 2016/7/3.
 */
public class SettingFragment extends Fragment implements View.OnClickListener {

    @Bind(R.id.title_layout)
    TitleView titleLayout;
    @Bind(R.id.profiles_tv)
    TextView profilesTv;
    @Bind(R.id.profiles_iv)
    ImageView profilesIv;
    @Bind(R.id.profiles_rl)
    RelativeLayout profilesRl;
    @Bind(R.id.account_management_tv)
    TextView accountManagementTv;
    @Bind(R.id.account_management_iv)
    ImageView accountManagementIv;
    @Bind(R.id.account_management_rl)
    RelativeLayout accountManagementRl;
    @Bind(R.id.check_update_tv)
    TextView checkUpdateTv;
    @Bind(R.id.check_update_iv)
    ImageView checkUpdateIv;
    @Bind(R.id.check_update_rl)
    RelativeLayout checkUpdateRl;
    @Bind(R.id.check_update_view)
    View checkUpdateView;
    @Bind(R.id.clear_cache_tv)
    TextView clearCacheTv;
    @Bind(R.id.clear_cache_iv)
    ImageView clearCacheIv;
    @Bind(R.id.common_problem_tv)
    TextView commonProblemTv;
    @Bind(R.id.common_problem_iv)
    ImageView commonProblemIv;
    @Bind(R.id.common_problem_rl)
    RelativeLayout commonProblemRl;
    @Bind(R.id.exit_login_btn)
    Button exitLoginBtn;
    private View mRootView;
    private Context mContext;

    private boolean isLogin = false;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_setting, null);
        }
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }
        ButterKnife.bind(this, mRootView);
        return mRootView;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.exit_login_btn, R.id.account_management_rl,R.id.profiles_rl,R.id.common_problem_rl})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.profiles_rl:
                startActivity(new Intent(getActivity(), ProfileActivity.class));
                break;
            case R.id.exit_login_btn:
                if (isLogin) {
                    showAlertDialog();
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            case R.id.account_management_rl:
                if (isLogin) {
                    startActivity(new Intent(getActivity(), AccountManagementActivity.class));
                } else {
                    ToastUtils.showShort("还未登录，请先登录");
                }
                break;
            case R.id.common_problem_rl:
                startActivity(new Intent(getActivity(), CommonProblemActivity.class));
                break;
            default:
                break;
        }
    }

    private void showAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("退出登录");
        builder.setMessage("是否退出登录？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                exitLoginBtn.setText("登录");
                isLogin = false;
                UserInfoSPManager.removeUserInfo();
            }
        });
        builder.setNegativeButton("取消",null);
        builder.show();
    }

    @Override
    public void onResume() {
        super.onResume();
        isLogin = UserInfoSPManager.getIsLogin();
        if (isLogin) {
            exitLoginBtn.setText("退出登录");
        } else {
            exitLoginBtn.setText("登录");
        }

    }
}